package org.shopen.ai.puremvc.model.utils
{
	public class Convert
	{
		private static const CDATA_BEGIN : String = "<![CDATA[";
		private static const CDATA_END : String = "]]>";
		
		public static function toString(value : Object = null) : String
		{
			var result : String = "";
			if (value != null)
			{
				result = value.toString();
			}
			return result;
		}
		
		public static function toBoolean(value : Object = null) : Boolean
		{
			var result : Boolean = false;
			if (value != null)
			{
				result = new Boolean(value);
			}
			return result;
		}
		
		public static function toNumber(value : Object = null) : Number
		{
			var result : Number = NaN;
			if (value != null)
			{
				result = Number(value);
			}
			return result;
		}
		
		public static function toInt(value : Object = null) : int
		{
			var result : int;
			if (value != null)
			{
				result = int(Convert.toNumber(value));
			}
			return result;
		}
		
		public static function toCDATA(value : String) : String
		{
			var result : String = CDATA_BEGIN + value + CDATA_END;
			return result;
		}
		
		public static function stripCDATA(value : String) : String
		{
			var result : String = value;
			if (result.indexOf(CDATA_BEGIN) == 0 && result.lastIndexOf(CDATA_END) == result.length - CDATA_END.length)
			{
				result = result.substr(0, CDATA_BEGIN.length);
				var indexOfCDATAEnd : Number = result.lastIndexOf(CDATA_END);
				result = result.substr(indexOfCDATAEnd);  
			}			
			return result;
		}
	}
}